//
//  RootViewController.m
//  Demo
//
//  Created by LAZYGUY on 25/08/2011.
//  Copyright 2011 HUTECH. All rights reserved.
//

#import "RootViewController.h"
#import "NewsCell.h"
#import "ParserRSS.h"


#define FONT_SIZE 13
#define LOAITIN_COUNT 14
#define CONTENT_MARGIN 4

@implementation RootViewController
@synthesize newsList,activityIndicator;
@synthesize waitingView;
@synthesize currentLoaiTin,myScrollView,titleArray,urlOfTitleArray;

- (void)viewDidLoad
{
    [super viewDidLoad];
        if (self.newsList == nil) {
            [waitingView setHidden:NO];
            [self.view addSubview:waitingView];
            [waitingView setFrame:CGRectMake(0, 41, 320, 460)];
            ParserRSS *rssParser = [[ParserRSS alloc] init];
            NSString *urlString = [[NSString alloc]initWithString:@"http://vnexpress.net/rss/gl/trang-chu.rss"];
//            NSString *urlString = [[NSString alloc]initWithString:@"http://tuoitre.vn/RssFeeds.aspx?ChannelID=3"];
            [rssParser parseRssFeed:urlString withDelegate:self];    
            [rssParser release];
            [urlString release];
        }else {
    		[self.tableView reloadData];
            [waitingView setHidden:YES];
    	}
        
	

    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
//    self.navigationItem.title = @"Tin tức";
    self.titleArray = [[NSArray alloc] initWithObjects:@"Trang chủ",@"Xã hội",@"Thế giới",@"Kinh doanh",@"Văn hóa",@"Thể thao",@"Pháp luật",@"Đời sống", @"Khoa học",@"Vi tính",@"Ô tô - Xe máy",@"Bạn đọc viết",@"Tâm sự" ,@"Cười ",nil];
    
    self.urlOfTitleArray = [[NSArray alloc] initWithObjects:
                            @"http://vnexpress.net/rss/gl/trang-chu.rss",
                            @"http://vnexpress.net/rss/gl/xa-hoi.rss",
                           @"http://vnexpress.net/rss/gl/the-gioi.rss",
                           @"http://vnexpress.net/rss/gl/kinh-doanh.rss",
                           @"http://vnexpress.net/rss/gl/van-hoa.rss",
                           @"http://vnexpress.net/rss/gl/the-thao.rss",
                           @"http://vnexpress.net/rss/gl/phap-luat.rss",
                           @"http://vnexpress.net/rss/gl/doi-song.rss", 
                           @"http://vnexpress.net/rss/gl/khoa-hoc.rss",
                           @"http://vnexpress.net/rss/gl/vi-tinh.rss",
                           @"http://vnexpress.net/rss/gl/oto-xe-may.rss",
                           @"http://vnexpress.net/rss/gl/ban-doc-viet.rss",
                           @"http://vnexpress.net/rss/gl/tam-su.rss"
                            @"http://vnexpress.net/rss/gl/cuoi.rss"
                            ,nil];
    

    // Init and setup UIScrollView
    self.myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
    self.myScrollView.showsHorizontalScrollIndicator = NO;
    self.myScrollView.backgroundColor = [UIColor blackColor];
    
    CGFloat contentWidth = 0.0f;
    for(int i = 0; i < LOAITIN_COUNT ; i++){
        
        CGSize size = [[self.titleArray objectAtIndex:i] sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE]];
        
        CGRect frame = CGRectMake(contentWidth + CONTENT_MARGIN, 3, size.width, size.height);
        
        contentWidth += size.width + CONTENT_MARGIN;
        
        UIButton *loaiTin = [UIButton buttonWithType:UIButtonTypeCustom];
        loaiTin.frame = frame;
        [loaiTin setBackgroundColor:[UIColor clearColor]];
        loaiTin.font = [UIFont systemFontOfSize:FONT_SIZE];
        [loaiTin setTitle:[self.titleArray objectAtIndex:i ] forState:UIControlStateNormal];
        
        loaiTin.tag = 1000 + i;
        [loaiTin addTarget:self action:@selector(btTheLoaiClick:) forControlEvents:UIControlEventTouchUpInside];
        [myScrollView addSubview:loaiTin];           
     self.myScrollView.contentSize = CGSizeMake(contentWidth, self.myScrollView.frame.size.height);
    [self.view addSubview:self.myScrollView];
    [self.navigationController.navigationBar addSubview:myScrollView];
    }
//    [self startparser:urlString];
}
-(void)btTheLoaiClick:(UIButton*)bt
{
    UIButton *currentBt = (UIButton*)[self.myScrollView viewWithTag:self.currentLoaiTin + 1000];
    [currentBt setSelected:NO];
    
    int index = bt.tag;
    self.currentLoaiTin = index%1000;
    [bt setSelected:YES];
    [bt setTitleColor:[UIColor yellowColor] forState:UIControlStateSelected];
    
    
    NSString *urlString = [NSString stringWithFormat:@"%@",[self.urlOfTitleArray objectAtIndex:self.currentLoaiTin]];
    NSLog(@"%@",urlString);
//    [self startparser:urlString];
}
//- (void) startparser:(NSString*)urlStr{
//    NSString *urlString = [NSString stringWithString:urlStr];
//    
//    
//
//       ParserRSS *rssParser = [[ParserRSS alloc] init];
//
//    [rssParser parseRssFeed:urlStr withDelegate:self];
//    
//    [rssParser release];
//    [urlString release];
//}
- (void)receivedItems:(NSArray *)theItems {
	self.newsList = [NSMutableArray arrayWithArray:theItems];
    NSLog(@"%i",[newsList count]);
    [waitingView setHidden:YES];
	[self.tableView reloadData];    
	
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}


// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   
    
    self.tableView.rowHeight=82;
    
    
//    [self.tableView setBackgroundColor:[UIColor clearColor]];
    return [newsList count];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CellID";
    static NSString *PlaceholderCellIdentifier = @"PlaceholderCellIdentifier";
    
    NewsCell *cell = (NewsCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *nib= [[NSBundle mainBundle] loadNibNamed:@"NewsCell" owner:self options:nil];
        for (id currentObject in nib)
        {
            if ([currentObject isKindOfClass:[UITableViewCell class]]) {
                cell =(NewsCell *)currentObject;
                break;
            }
        }
        
    }
    
    // Configure the cell.
    
    cell.titleNews.text = [[newsList objectAtIndex:[indexPath row]] titleNews];
    cell.summaryLabel.text = [[newsList objectAtIndex:[indexPath row]] summaryNews];
    if ([[newsList objectAtIndex:[indexPath row]]imageNews]!=nil) {
    cell.imageNews.image = [[newsList objectAtIndex:[indexPath row]] imageNews];
    }else{
        cell.imageNews.image = [UIImage imageNamed:@"noImage.gif"];
    }
    return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    /*
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
    // ...
    // Pass the selected object to the new view controller.
    [self.navigationController pushViewController:detailViewController animated:YES];
    [detailViewController release];
	*/
    
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc that aren't in use.
}


- (void)dealloc
{
    [super dealloc];
    [newsList release];
    
}

@end
